﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;

using HANDLE = System.IntPtr;
using DWORD = System.UInt32;

namespace ActiveDirectoryUtilities.CommonLibrary
{
    static class UnsafeCode
    {
        [DllImport("Kernel32", SetLastError = true)]
        private static extern HANDLE GetStdHandle(DWORD nStdHandle);

        [DllImport("Kernel32", SetLastError = true)]
        private static extern bool PeekConsoleInput(HANDLE hConsoleInput, IntPtr lpBuffer, DWORD nLength, out DWORD lpNumberOfEventsRead);

        private const DWORD STD_INPUT_HANDLE = unchecked((DWORD)(-10));

        public static bool stdinRedirected
        {
            get
            {
                HANDLE stdinHandle = GetStdHandle(STD_INPUT_HANDLE);
                DWORD numOfEventsRead;
                return !PeekConsoleInput(stdinHandle, IntPtr.Zero, 0, out numOfEventsRead);
            }
        }
    }
}
